package cn.com.pc.content.controller;

import cn.com.pc.content.annotation.LogRequired;
import cn.com.pc.content.domain.Biz;
import cn.com.pc.content.domain.Site;
import cn.com.pc.content.domain.tag.Tag;
import cn.com.pc.content.domain.tag.TagRelation;
import cn.com.pc.content.domain.tag.TagRelationNotFoundException;
import cn.com.pc.content.dto.PageForm;
import cn.com.pc.content.dto.PageResult;
import cn.com.pc.content.dto.TagRelationDTO;
import cn.com.pc.content.service.TagRelationService;
import cn.com.pc.content.service.TagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.*;

/**
 * 标签关系管理
 *
 */
@RestController
@RequestMapping("/tag-relations")
public class TagRelationController {

    @Autowired
    private TagRelationService tagRelationService;

    @Autowired
    private TagService tagService;


    /**
     * 新增或修改标签关系
     * @param tagRelationDTO
     * @return
     */
    @PostMapping
    @LogRequired(operation = "'新增标签关系' + 'leftId:' + #tagRelationDTO.getLeftId() + 'rightId:' + #tagRelationDTO.getRightId()")
    public TagRelation createOrUpdateTagRelation(@Validated @RequestBody TagRelationDTO tagRelationDTO) {
        Tag left = tagService.getTagById(tagRelationDTO.getLeftId());
        Tag right = tagService.getTagById(tagRelationDTO.getRightId());
        if (left == null || right == null) {
            throw new TagRelationNotFoundException();
        }
        tagRelationDTO.setCreatedAt(LocalDateTime.now());
        return tagRelationService.createOrUpdateTagRelation(tagRelationDTO, left, right);
    }


    /**
     * 删除标签关系
     */
    @DeleteMapping("/{leftId:\\d+}/{rightId:\\d+}")
    @LogRequired(operation = "'删除标签关系' + 'leftId:' + #leftId + 'rightId:' + #rightId")
    public void deleteTagRelation(@PathVariable Long leftId, @PathVariable Long rightId) {
        Tag left = tagService.getTagById(leftId);
        Tag right = tagService.getTagById(rightId);
        if (left == null || right == null) {
            throw new TagRelationNotFoundException();
        }
        tagRelationService.deleteTagRelation(left, right);
    }


    /**
     * 查询标签关系列表
     * @param tagRelationDTO
     * @param pageForm
     * @return
     */
    @GetMapping
    public PageResult<TagRelation> listTagRelationsByDTO(@RequestAttribute(value = "site", required = false) Site site, @RequestAttribute(value = "biz", required = false) Biz biz, TagRelationDTO tagRelationDTO, PageForm pageForm) {
        if (tagRelationDTO.getLeftId() == null && tagRelationDTO.getLeftName() != null && site != null && biz != null) {
            Tag leftTag = tagService.getTagByName(site, biz, tagRelationDTO.getLeftName());
            if (leftTag != null) {
                tagRelationDTO.setLeftId(leftTag.getId());
            }
        }
        if (tagRelationDTO.getRightId() == null && tagRelationDTO.getRightName() != null && site != null && biz != null) {
            Tag rightTag = tagService.getTagByName(site, biz, tagRelationDTO.getLeftName());
            if (rightTag != null) {
                tagRelationDTO.setRightId(rightTag.getId());
            }
        }
        return tagRelationService.listTagRelationsByDTO(tagRelationDTO, pageForm);
    }


    /**
     * 查询标签关系链
     * @return
     */
    @GetMapping("/chains")
    public List<?> getTagRelationChain(@RequestParam List<Long> tagIds, @RequestParam(required = false) boolean parents, @RequestParam(required = false) boolean sons, @RequestParam(required = false, defaultValue = "5") int limit) {
        List<Map<String, Object>> result = new ArrayList<>();
        for (Long tagId : tagIds) {
            List<Object> tagParents = Collections.emptyList();
            List<Object> tagSons = Collections.emptyList();
            if (parents) {
                tagParents = tagRelationService.getTagParents(tagId, limit);
            }
            if (sons) {
                tagSons = tagRelationService.getTagSons(tagId, limit);
            }
            Map<String, Object> entity = new HashMap<>();
            entity.put("tagId", tagId);
            entity.put("tag", tagService.getTagById(tagId));
            entity.put("parents", tagParents);
            entity.put("sons", tagSons);
            result.add(entity);
        }
        return result;
    }

}
